﻿using Forum.BaseController;
using Forum.Infrastructure;
using Forum.Service.Interface;
using Microsoft.AspNetCore.Authorization;

namespace Forum.Controllers
{
    /// <summary>
    /// 文章分类
    /// </summary>
    [Route("Api/Category")]
    public class CategoryController : BaseController<ICategoryService>
    {
        public CategoryController(ICategoryService defaultService, ILogger<BaseController<ICategoryService>> logger) : base(defaultService, logger)
        {
        }

        [HttpGet("Nodes")]
        public ApiResponse<List<CategoryNode>> GetNodes()
        {
            return new ApiResponse<List<CategoryNode>>(DefaultService.GetNodes(nameof(BlogController)));
        }

        /// <summary>
        /// 查询所有分类
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("All")]
        public ApiResponse<List<Category>> GetAll()
        {
            return new ApiResponse<List<Category>>(DefaultService.GetAll());
        }

        /// <summary>
        /// 查询分类（分页）
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public ApiResponsePaged<Category> GetList(int page = 1, int pageSize = 10)
        {
            var paged = DefaultService.GetPagedList(page, pageSize);
            return new ApiResponsePaged<Category>
            {
                data = paged.ToList(),
                total = paged.Total
            };
        }

        /// <summary>
        /// 查询单个分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("{id:int}")]
        public IApiResponse Get(int id)
        {
            var item = DefaultService.GetById(id);
            return item == null ? ApiResponse.NotFound() : new ApiResponse<Category> { data = item };
        }

        /// <summary>
        /// 分类词云
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("[action]")]
        public ApiResponse<List<object>> WordCloud()
        {
            return new ApiResponse<List<object>>(DefaultService.GetWordCloud());
        }

        /// <summary>
        /// 设置分类可见
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{id:int}/[action]")]
        public ApiResponse SetVisible(int id)
        {
            var item = DefaultService.GetById(id);
            if (item == null) return ApiResponse.NotFound($"分类 {id} 不存在");
            DefaultService.SetVisibility(item, true);
            return ApiResponse.OK();
        }

        /// <summary>
        /// 设置分类不可见
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{id:int}/[action]")]
        public ApiResponse SetInvisible(int id)
        {
            var item = DefaultService.GetById(id);
            if (item == null) return ApiResponse.NotFound($"分类 {id} 不存在");
            DefaultService.SetVisibility(item, false);
            return ApiResponse.OK();
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public ApiResponse Delete(int id)
        {
            var post = DefaultService.GetById(id);
            if (post == null) return ApiResponse.NotFound($"该分类不存在");
            DefaultService.Delete(id);
            return ApiResponse.OK();
        }

        /// <summary>
        /// 新增分类
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("Add")]
        public ApiResponse<Category> Add(CategoryReq req)
        {
            var category = req.MapTo<CategoryReq, Category>();

            return new ApiResponse<Category>(DefaultService.Insert(category));
        }

        /// <summary>
        /// 更新分类
        /// </summary>
        /// <param name="id"></param>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public ApiResponse<Category> Update(int id, CategoryReq req)
        {
            var category = DefaultService.GetById(id);
            if (category == null) return ApiResponse.NotFound("该分类不存在");

            var item = req.MapTo<CategoryReq, Category>();

            return new ApiResponse<Category>(DefaultService.Update(item));
        }
    }
}
